package com.fe.class06;

import java.util.*;

/**
 * @Description 比较器，排序可以自定义比较器
 * 1)比较器的实质就是重载比较运算符
 * 2)比较器可以很好的应用在特殊标准的排序上
 * 3)比较器可以很好的应用在根据特殊标准排序的结构上
 * 4)写代码变得异常容易，还用于范型编程
 * @Author sosuke :-)
 * @Date 2021/12/28 06:36
 */
public class Code01_Comparator {
    public static class Student {
        public String name;
        public int id;
        public int age;

        public Student(String name, int id, int age) {
            this.name = name;
            this.id = id;
            this.age = age;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", id=" + id +
                    ", age=" + age +
                    '}';
        }
    }

    /**
     * 任何比较器：
     * 	compare方法里，遵循一个统一的规范：
     * 	返回负数的时候，认为第一个参数应该排在前面
     * 	返回正数的时候，认为第二个参数应该排在前面
     * 	返回0的时候，认为无所谓谁放前面
     */
    public static class IdAscendingComparator implements Comparator<Student> {
        @Override
        public int compare(Student o1, Student o2) {
            if (o1.id > o2.id) {
                return 1; // 返回正数的时候，认为第二个参数应该排在前面
            } else if (o1.id < o2.id) {
                return -1;
            } else {
                return 0;
            }
            // return o1.id - o2.id;
        }
    }

    public static void main(String[] args) {
        Integer[] arr = { 5, 4, 3, 2, 7, 9, 1, 0 };
        Arrays.sort(arr, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        System.out.println(Arrays.toString(arr));
        System.out.println("===========================");
        Student student1 = new Student("A", 4, 40);
        Student student2 = new Student("B", 4, 21);
        Student student3 = new Student("C", 3, 12);
        Student student4 = new Student("D", 3, 62);
        Student student5 = new Student("E", 3, 42);
        Student[] students = new Student[] { student1, student2, student3, student4, student5 };
        System.out.println("第一条打印");
        Arrays.sort(students, new IdAscendingComparator());
        System.out.println(Arrays.toString(students));

        System.out.println("第二条打印");
        ArrayList<Student> studentList = new ArrayList<>();
        studentList.add(student1);
        studentList.add(student2);
        studentList.add(student3);
        studentList.add(student4);
        studentList.add(student5);
        // 根据id从小到大，但是如果id一样，按照年龄从大到小
        studentList.sort((a,b) -> a.id != b.id ? a.id - b.id : b.age - a.age);
        studentList.forEach(System.out::println);

        // 有序表，定义比较器，不允许加重复key
        System.out.println("第三条打印");
        TreeMap<Student, String> treeMap = new TreeMap<>((a,b) -> b.id - a.id);
        treeMap.put(student1,"我是学生1");
        treeMap.put(student2,"我是学生2");
        treeMap.put(student3,"我是学生3");
        treeMap.put(student4,"我是学生4");
        treeMap.put(student5,"我是学生5");
        System.out.println(treeMap.firstKey());
        for (Student stu : treeMap.keySet()) {
            System.out.println(stu);
        }
    }
}
